package com.sdhs.workflow.utils;

import com.alibaba.fastjson2.JSONObject;
import com.sdhs.workflow.utils.SignUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DemoTests {


    /**
     * 新增群组
     */
    void add() {
        // 业务参数
        JSONObject details = new JSONObject();
        details.put("isUnifiedUser",true);
        // 业务系统标识
        details.put("appId", "业务系统标识");
        // 创建组所需信息
        JSONObject group_info = new JSONObject();
        // 群组名称
        group_info.put("name", "穿透门户管理员");
        // 群组所属消息类型
        // 站内信-LETTER，短信-SMS，邮件-EMAIL，企业微信-WXWORK
        group_info.put("msgType", "SMS");
        // 创建人ID，用户管理平台用户ID
        group_info.put("creator", "创建人ID");
        // 备注
        group_info.put("remark", "");
        details.put("group_info", group_info);

        // 创建组成员所需信息
        List<JSONObject> receivers = new ArrayList<>();
        // 成员1
        JSONObject receiver1 = new JSONObject();
        // 类型，01-用户管理平台用户ID，02-自定义接收地址
        receiver1.put("type", "01");
        // 接收地址
        receiver1.put("receive_addr", "用户管理平台用户ID");
        // 联系人详情
        receiver1.put("receive_desc", "");
        // 备注
        receiver1.put("remark", "");
        receivers.add(receiver1);
        // 成员2
        JSONObject receiver2 = new JSONObject();
        // 类型，01-用户管理平台用户ID，02-自定义接收地址
        receiver2.put("type", "01");
        // 接收地址
        receiver2.put("receive_addr", "用户管理平台用户ID");
        // 联系人详情
        receiver2.put("receive_desc", "");
        // 备注
        receiver2.put("remark", "");
        receivers.add(receiver2);
        // 成员3
        JSONObject receiver3 = new JSONObject();
        // 类型，01-用户管理平台用户ID，02-自定义接收地址
        receiver3.put("type", "02");
        // 接收地址，邮件为邮箱，短信为手机号
        receiver3.put("receive_addr", "自定义接收地址");
        // 联系人详情，一般为接收人姓名
        receiver3.put("receive_desc", "");
        // 备注
        receiver3.put("remark", "");
        receivers.add(receiver3);
        details.put("receivers", receivers);

        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "系统密钥", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        long currentTimestampMs = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(currentTimestampMs));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    /**
     * 向已存在的群组添加成员
     */
    void addMembers() {
        // 业务参数
        JSONObject details = new JSONObject();
        details.put("isUnifiedUser",true);
        // 业务系统标识
        details.put("appId", "业务系统标识");
        // 创建人ID，用户管理平台用户ID
        details.put("creator", "创建人ID");
        // 群组ID
        details.put("group_id", "群组ID");

        // 创建组成员所需信息
        List<JSONObject> receivers = new ArrayList<>();
        // 成员1
        JSONObject receiver1 = new JSONObject();
        // 类型，01-用户管理平台用户ID，02-自定义接收地址
        receiver1.put("type", "01");
        // 接收地址
        receiver1.put("receive_addr", "用户管理平台用户ID");
        // 联系人详情
        receiver1.put("receive_desc", "");
        // 备注
        receiver1.put("remark", "");
        receivers.add(receiver1);
        // 成员2
        JSONObject receiver2 = new JSONObject();
        // 类型，01-用户管理平台用户ID，02-自定义接收地址
        receiver2.put("type", "01");
        // 接收地址
        receiver2.put("receive_addr", "用户管理平台用户ID");
        // 联系人详情
        receiver2.put("receive_desc", "");
        // 备注
        receiver2.put("remark", "");
        receivers.add(receiver2);
        // 成员3
        JSONObject receiver3 = new JSONObject();
        // 类型，01-用户管理平台用户ID，02-自定义接收地址
        receiver3.put("type", "02");
        // 接收地址，邮件为邮箱，短信为手机号
        receiver3.put("receive_addr", "自定义接收地址");
        // 联系人详情，一般为接收人姓名
        receiver3.put("receive_desc", "");
        // 备注
        receiver3.put("remark", "");
        receivers.add(receiver3);
        details.put("receivers", receivers);

        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "系统密钥", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        long currentTimestampMs = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(currentTimestampMs));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    /**
     * 根据群组ID查询群组信息以及成员信息
     */
    void queryById() {
        // 业务参数
        JSONObject details = new JSONObject();
        // 业务系统标识
        details.put("appId", "业务系统标识");
        details.put("group_id", "群组ID");
        details.put("isUnifiedUser",true);

        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "系统密钥", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        long currentTimestampMs = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(currentTimestampMs));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    /**
     * 查询业务系统标识下所有群组及成员信息
     */
    void queryList() {
        // 业务参数
        JSONObject details = new JSONObject();
        details.put("appId", "业务系统标识");
        details.put("isUnifiedUser",true);

        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "系统密钥", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        long currentTimestampMs = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(currentTimestampMs));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    /**
     * 根据群组ID删除群组及成员信息
     */
    void delete() {
        // 业务参数
        JSONObject details = new JSONObject();
        details.put("appId", "业务系统标识");
        details.put("group_id", "群组ID");

        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "系统密钥", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        long currentTimestampMs = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(currentTimestampMs));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    /**
     * 删除群组下某个成员信息
     */
    void deleteMembers() {
        // 业务参数
        JSONObject details = new JSONObject();
        details.put("isUnifiedUser",true);
        details.put("appId", "业务系统标识");
        details.put("group_id", "群组ID");
        List<String> receive_ids = new ArrayList<>();
        receive_ids.add("成员ID");
        receive_ids.add("成员ID");

        details.put("receive_ids", receive_ids);
        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "系统密钥", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        long currentTimestampMs = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(currentTimestampMs));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    /**
     * 发送站内信
     */
    void sendInstation() {
        // 业务参数
        JSONObject details = new JSONObject();
        details.put("isUnifiedUser",true);
        details.put("appId", "业务系统标识");
        // 消息类型
        details.put("type", "LETTER");
        // 收件人信息
        JSONObject receivers = new JSONObject();
        List<String> orgId = new ArrayList<>();
        orgId.add("机构/部门ID");
        receivers.put("orgId", orgId);
        List<String> groupId = new ArrayList<>();
        groupId.add("群组ID");
        receivers.put("groupId", groupId);
        List<String> userId = new ArrayList<>();
        userId.add("用户ID");
        receivers.put("userId", userId);
        details.put("receivers", receivers);
        // 标题
        details.put("title", "站内信发送测试");
        // 内容
        details.put("content", "这是一条站内信发送测试");
        // 发件人
        details.put("msgCreator", "发件人ID");
        // 备注
        details.put("remark", "这是一条站内信发送测试的备注");
        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "系统密钥", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        long currentTimestampMs = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(currentTimestampMs));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    /**
     * 发送邮件
     */
    void sendMail() {
        // 业务参数
        JSONObject details = new JSONObject();
        details.put("isUnifiedUser",true);
        details.put("appId", "业务系统标识");
        details.put("type", "EMAIL");
        details.put("title", "邮件发送测试");
        // 收件人信息
        JSONObject receivers = new JSONObject();
        List<String> orgId = new ArrayList<>();
        orgId.add("机构/部门ID");
        receivers.put("orgId", orgId);
        List<String> groupId = new ArrayList<>();
        groupId.add("群组ID");
        receivers.put("groupId", groupId);
        List<String> userId = new ArrayList<>();
        userId.add("用户ID");
        receivers.put("userId", userId);
        // 自定义地址
        List<String> receiveAddr = new ArrayList<>();
        receiveAddr.add("邮箱地址");
        receivers.put("receiveAddr", receiveAddr);
        details.put("receivers", receivers);

        // 抄送人信息copiedReceivers
        JSONObject copiedReceivers = new JSONObject();
        List<String> copiedOrgId = new ArrayList<>();
        copiedOrgId.add("组织/部门ID");
        copiedReceivers.put("groupId", copiedOrgId);
        List<String> copiedGroupId = new ArrayList<>();
        copiedGroupId.add("群组ID");
        copiedReceivers.put("groupId", copiedGroupId);
        List<String> copiedUserId = new ArrayList<>();
        copiedUserId.add("用户ID");
        copiedReceivers.put("userId", copiedUserId);
        List<String> copiedReceiveAddr = new ArrayList<>();
        copiedReceiveAddr.add("邮箱地址");
        copiedReceivers.put("receiveAddr", copiedReceiveAddr);
        details.put("copiedReceivers", copiedReceivers);
        details.put("content", "这是一条邮件发送测试");
        details.put("msgCreator", "发件人用户ID");
        details.put("remark", "这是一条邮件发送测试的备注");
        // 附件详情
        List<JSONObject> attList = new ArrayList<>();
        JSONObject att = new JSONObject();
        // 附件地址
        att.put("url", "https://images.shobserver.com/news/690_390/2021/8/8/aed71a374b734ae1ac3930bebdba9c87.jpg");
        // 附件名称
        att.put("name", "上观时政");
        attList.add(att);
        details.put("attList", attList);
        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "系统密钥", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        long currentTimestampMs = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(currentTimestampMs));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    /**
     * 发送短信
     */
    void sendSms() {
        // 业务参数
        JSONObject details = new JSONObject();
        details.put("isUnifiedUser",true);
        details.put("appId", "业务系统标识");
        details.put("type", "SMS");
        details.put("title", "短信发送测试");
        // 收件人信息
        // 收件人信息
        JSONObject receivers = new JSONObject();
        List<String> orgId = new ArrayList<>();
        orgId.add("机构/部门ID");
        receivers.put("groupId", orgId);
        List<String> groupId = new ArrayList<>();
        groupId.add("群组ID");
        receivers.put("groupId", groupId);
        List<String> userId = new ArrayList<>();
        userId.add("用户ID");
        receivers.put("userId", userId);
        // 自定义地址
        List<String> receiveAddr = new ArrayList<>();
        receiveAddr.add("手机号");
        receivers.put("receiveAddr", receiveAddr);
        details.put("receivers", receivers);

        details.put("content", "这是一条短信发送测试");
        details.put("msgCreator", "发件人用户ID");
        details.put("remark", "这是一条短信发送测试的备注");
        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "系统密钥", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        long currentTimestampMs = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(currentTimestampMs));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    void pushTodo(){
        // 业务参数
        JSONObject details = new JSONObject();
        details.put("isUnifiedUser",true);
        details.put("type","URL");
        details.put("appId","1676495326170099713");
        details.put("bizId","111111");
        details.put("code","code111111");
        details.put("title","测试流程");
        details.put("content","流程内容");
        details.put("currentHandler","222222");
        details.put("flowCreator","33333");
        details.put("taskId","44444");
        details.put("flowUrl","www.baidu.com");
        details.put("remark","备注");

        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "CKaFsvdscLX6NSzjh3ndIAFEhNuh7cYr", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
            e.printStackTrace();
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        requestParams.put("isUnifiedUser", true);
        long timestamp = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(timestamp));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    void delTodo(){
        // 业务参数
        JSONObject details = new JSONObject();
        details.put("appId","1676495326170099713");
        details.put("taskId","44444");

        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "CKaFsvdscLX6NSzjh3ndIAFEhNuh7cYr", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
            e.printStackTrace();
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        requestParams.put("isUnifiedUser", true);
        long timestamp = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(timestamp));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    void setTodo(){
        // 业务参数
        JSONObject details = new JSONObject();
        details.put("appId","1676495326170099713");
        details.put("taskId","44444");

        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "CKaFsvdscLX6NSzjh3ndIAFEhNuh7cYr", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
            e.printStackTrace();
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        requestParams.put("isUnifiedUser", true);
        long timestamp = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(timestamp));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    void pushMsg(){
        // 业务参数
        JSONObject details = new JSONObject();
        details.put("isUnifiedUser",true);
        details.put("type","WXWORK");
        details.put("appId","1676495326170099713");
        details.put("bizId","55555");
        details.put("code","code66666");
        details.put("title","测试流程");
        details.put("content","流程内容");
        details.put("msgCreator","7777");
        details.put("flowCreator","8888");
        details.put("taskId","9999");
        JSONObject receivers = new JSONObject();
        List<String> list = new ArrayList<>();
        list.add("123456");
        list.add("234567");
        receivers.put("userId",list);
        details.put("receivers",receivers);
        details.put("remark","备注");

        String nonceStr = SignUtil.generateNonceStr();
        String sign = null;
        try {
            sign = SignUtil.generateSignature(details, "CKaFsvdscLX6NSzjh3ndIAFEhNuh7cYr", nonceStr);
        } catch (Exception e) {
            // 根据业务需求处理异常
            e.printStackTrace();
        }

        // 接口请求参数
        JSONObject requestParams = new JSONObject();
        requestParams.put("sign", sign);
        requestParams.put("nonceStr", nonceStr);
        requestParams.put("isUnifiedUser", true);
        long timestamp = SignUtil.getCurrentTimestampMs();
        requestParams.put("timestamp", String.valueOf(timestamp));
        requestParams.put("details", details);
        // 最终请求参数模板
        System.out.println(requestParams.toJSONString());
    }

    public static void main(String[] args)
    {
        DemoTests demo = new DemoTests();
        demo.pushMsg();
    }
}
